package com.sservice.urp.controller;

import com.sservice.urp.e.CategoryRelation;
import com.sservice.urp.service.CategoryRelationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sservice.base.entity.dto.BaseResult;
import com.sservice.base.entity.dto.ListResult;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by byte_su@163.com on 2018/01/12.
 */
@Controller
@RequestMapping("/admin/categoryRelation/")
public class CategoryRelationController {

    @Autowired
    CategoryRelationService categoryRelationService;

    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult add(CategoryRelation categoryRelation) {
        return new BaseResult(true, String.valueOf(categoryRelationService.save(categoryRelation)), 200);
    }

    /**
     * 批量修改
     *
     * @param menuItem
     * @return
     */
    @RequestMapping(value = "batchUpdate", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult addBatch(CategoryRelation menuItem, String ids) {
        if (ids != null && !"".equals(ids)) { // 批量删除
            String[] ids1 = ids.split(",");
            for (String id : ids1) {
                categoryRelationService.deleteById(Integer.valueOf(id));
            }
        }
        List list = new ArrayList();
        if (menuItem.getItemId() != null && !"".equals(menuItem.getItemId())) {
            String[] pids = menuItem.getItemId().split(",");
            for (String pid : pids) {
                CategoryRelation mi = new CategoryRelation();
                mi.setCategoryId(menuItem.getCategoryId());
                mi.setType1(menuItem.getType1());
                mi.setItemId(pid);
                list.add(mi);
            }
            return new BaseResult(true, String.valueOf(categoryRelationService.save(list)), 200);
        }
        return new BaseResult(true, "", 200);
    }


    @RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult delete(@RequestParam Integer id) {
        return new BaseResult(true, String.valueOf(categoryRelationService.deleteById(id)), 200);
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult update(CategoryRelation categoryRelation) {
        return new BaseResult(true, String.valueOf(categoryRelationService.update(categoryRelation)), 200);
    }

    @RequestMapping("detail/{id}")
    @ResponseBody
    public BaseResult detail(@RequestParam Integer id) {
        CategoryRelation categoryRelation = categoryRelationService.findById(id);
        return new BaseResult(true, categoryRelation);
    }

    @RequestMapping("list")
    @ResponseBody
    public ListResult list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<CategoryRelation> list = categoryRelationService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return new ListResult(true, pageInfo);
    }
}
